package top.kaoshanji.p2ch15b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 代码清单-p2ch15b-456页b：同时开始的主类
 * author kaoshanji
 * ^_^
 * create time 2025/2/24 17:14
 */
public class RacerDemo {

    private static final Logger logger = LogManager.getLogger(RacerDemo.class);

    public static void main(String[] args) throws InterruptedException {
        logger.info("...start...");

        int num = 10;

        // 协作的共享变量：多个子线程（阻塞）、主线程
        RacerFireFlag fireFlag = new RacerFireFlag();

        Thread[] racers = new Thread[num];
        for (int i = 0; i < num; i++) {
            racers[i] = new RacerThread(fireFlag);
            racers[i].start(); // 被阻塞...等待条件改变才能继续执行
        }

        logger.info("...被阻塞了...");

        Thread.sleep(1000);
        // 发出信号，让子线程开始
        fireFlag.fire(); // 改变条件，让条件满足

        logger.info("...end...");
    }

}
